Skip to content

fix(mcpx): supervisor per-connection writer + bounded write deadline (#2390) - #2861

Merged
dezren39 merged 3 commits into
mainfrom
queue/3978-2390-supervisor-writer
Oct 31, 2026
Merged

dezren39 merged 3 commits into
mainfrom
queue/3978-2390-supervisor-writer

Conversation

@dezren39

@dezren39 dezren39 commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Closes #2390. Implements the triage settlements from queue task 3559 exactly:

  • No blocking write under s.mu: new per-connection connWriter (own mutex + conn). pumpStdout only snapshots the writer pointer under s.mu; handleAttach publishes the writer under s.mu and releases it before writing.
  • Ordering: the writer mutex is held across the publish + reply/buffered flush, so the reply → buffered → live ordering the old under-lock flush protected is preserved with no interleave.
  • Stall bound: every attached write carries a deadline (AttachedWriteTimeout, default DefaultAttachedWriteTimeout = 5s, package-level and test-overridable), set before each write. On expiry/error the chunk goes to the tail buffer, the attach is cleared only if it still points at that same writer (pointer identity), and the conn is closed — so the pump drains the child pipe to EOF, the recorded exit surfaces, and detach/re-attach proceed.
  • Tests (supervised_test.go): TestSupervisedStalledReaderAttachDropped — a daemon that never reads, a child emitting ~5 MiB then exiting; asserts RunSupervisor finishes within a timeout and the stalled connection was closed by the supervisor. TestSupervisedAttachFlushOrdering — raw attach asserts reply first, then the 100-frame buffered burst in order, then a live echo. New scripted children: floodchild, burstchild.

Untouched per scope: wire protocol/handshake version, tail-buffer sizing and droppedBytes accounting, the client-side transport, and the default-off supervised flag.

Verification is CI-first: this PR's checks run the mcpx suite (including -race coverage in the mcpx workflows). Queue task 3978.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

@dezren39
dezren39 requested a review from a team as a code owner October 30, 2026 23:23
@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 28 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d23b4af1-b72f-4551-88bf-3bca2292075a
Commits

Reviewing files that changed from the base of the PR and between 0744857 and f83a26a.

Files selected for processing: 2
  • pkgs/mcpx/internal/mcpclient/supervised.go
  • pkgs/mcpx/internal/mcpclient/supervised_test.go
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-31T00:52:04.090561Z f83a26a Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8b3251ea27

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 372 to +375
s.mu.Lock()
if s.attached != nil {
if _, werr := s.attached.Write(data); werr != nil {
w := s.attached
s.mu.Unlock()
if w != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep the unattached decision atomic with buffering

When stdout arrives concurrently with an attachment, the pump can snapshot s.attached == nil and release s.mu, then handleAttach can drain the buffer and publish its writer before the pump reaches s.buf.Write(data). That already-read chunk is consequently left in the buffer until another attachment, while subsequent chunks go directly to the current daemon, withholding output and reversing stream order. Keep the nil-writer decision and buffer append atomic relative to the attach drain/publication; the per-writer mutex only orders chunks routed to that writer.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid — fixing. The unattached path will keep the nil-writer decision and the tail-buffer append atomic under s.mu (a memory append, not a connection write): the pump holds s.mu across buf.Write when the snapshot is nil, so an already-read chunk is either inside the drain an attach flushes, or routed to the published writer — never stranded in the buffer behind a live stream. The per-writer mutex continues to order only chunks routed to a writer. Fix commit follows as soon as the laptop worktree is reachable again (SSH tunnel is down this shift); CI on the current head is fully green, and this thread stays open until the fix lands.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3a272fb (pushed): the pump now holds s.mu across the tail-buffer append when the snapshot writer is nil — the nil decision and buf.Write are atomic relative to handleAttach drain/publish, so a just-read chunk is either inside the drained buffer or routed to the published writer. buf.Write is a bounded memory append under the buffer's own mutex, not a connection write, so the #2390 no-write-under-s.mu rule stands. Verified on the laptop: go test ./internal/mcpclient/ -run TestSupervised -race PASS; full package PASS.

Comment on lines 466 to +468
s.mu.Unlock()
werr := w.flushLocked(reply, buffered)
w.mu.Unlock()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Coordinate child-exit closure with the initial flush

If the handshake snapshots ChildState == "running" and the child exits without further stdout while the initial buffered flush is in progress, pumpDone can close without waiting on this writer. finishWhenDone then sees attachedEver == true and reported == false and immediately closes the connection, interrupting the handshake or buffered flush and losing bytes already drained from the tail buffer. Previously, holding s.mu through the flush prevented that closure. Coordinate final connection closure with completion of the initial flush without restoring socket writes under s.mu.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid — fixing. finishWhenDone will take a flush-completion barrier on the connWriter before closing the attached connection: acquiring the writer mutex (which the initial flush holds across publish+flush, and which is bounded by the write deadline) and releasing it before Close. The stdout pump is already done at that point, so the only writer the barrier can wait on is the initial flush — closure can no longer interrupt it, and no socket write returns to s.mu. Fix commit follows as soon as the laptop worktree is reachable again (SSH tunnel is down this shift); this thread stays open until the fix lands.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3a272fb (pushed): connWriter.closeAfterFlush() — finishWhenDone now acquires/releases the writer mutex (the barrier the initial flush holds across publish+flush, bounded by AttachedWriteTimeout) before closing the attached connection, at both close sites in finishWhenDone. The pump has exited at those points (pumpDone), so the flush is the only possible holder; no socket write returns to s.mu. Verified on the laptop: go test ./internal/mcpclient/ -run TestSupervised -race PASS; full package PASS.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3a272fbb7d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +415 to +418
s.buf.Write(data)
s.attached.Close()
s.attached = nil
s.mu.Lock()
if s.attached == w {
s.attached = nil

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Route failed old-writer chunks through the current attachment

If a daemon detaches while a stdout write is outstanding, detach can clear the old writer and a new attachment can drain the buffer and publish its writer before the old write returns an error. This error path then appends the older chunk to the tail buffer even though the new attachment is live; subsequent chunks reach the new daemon directly, leaving the older output stranded until another attachment and reversing stream order. A controlled reproduction produced live B\n on the replacement connection with A\n left buffered. Coordinate recovery of failed writes with attachment drain/publication, including when the failed writer has already been replaced.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in the follow-up commit (pushed): the recovery path now snapshots the current writer under s.mu — with no replacement, the tail-buffer append happens under s.mu (atomic with the next attach drain, same as the unattached path); with a replacement writer already published, the chunk is routed to that writer outside s.mu, serialised on its mutex behind its initial flush, so it precedes later live chunks instead of stranding in the buffer. The pump is single-goroutine, so routing within the same iteration preserves stream order. If the replacement write also fails, the chunk falls back to the buffer and that writer is dropped the same way. Laptop: go test ./internal/mcpclient/ -race (supervised subset) and full package PASS.

@dezren39

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: f83a26a29e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@dezren39
dezren39 merged commit 206f4fd into main Oct 31, 2026
11 checks passed
@dezren39
dezren39 deleted the queue/3978-2390-supervisor-writer branch October 31, 2026 01:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant